package com.lx.algorithm;

import java.util.HashMap;
import java.util.HashSet;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2022-01-04 18:22:23
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2022-01-04     张李鑫                     1.0         1.0 Version
 */
public class Utils {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> set=new HashMap();
        String a="";
        for(int i=0;i<=nums.length-1;i++){

            if(set.get(target-nums[i])!=null){
                return new int []{i,set.get(target-nums[i])};
            }
            set.put(nums[i],i);
        }
        return new int[2];
    }

    public static void main(String[] args) {
        int []arr={3,2,4};

        int[] ints = new Utils().twoSum(arr, 6);
        System.out.println(ints[0]);
        System.out.println(ints[1]);
    }

    // for test
    public static int[][] generateRandomMatrix(int rowSize, int colSize) {
        if (rowSize < 0 || colSize < 0) {
            return null;
        }
        int[][] result = new int[rowSize][colSize];
        for (int i = 0; i != result.length; i++) {
            for (int j = 0; j != result[0].length; j++) {
                result[i][j] = (int) (Math.random() * 10);
            }
        }
        return result;
    }

    public static int[][] generateRandomMatrixII(int rowSize, int colSize) {
        if (rowSize < 0 || colSize < 0) {
            return null;
        }
        int[][] result = new int[rowSize][colSize];
        for (int i = 0; i != result.length; i++) {
            for (int j = 0; j != result[0].length; j++) {
                result[i][j] = (int) (Math.random() * 10);
            }
        }
        return result;
    }


    public static int[] generateRandomMatrix(int rowSize) {
        if (rowSize < 0) {
            return null;
        }
        int[] result = new int[rowSize];
        for (int i = 0; i != result.length; i++) {
            result[i] = (int) (Math.random() * 10)+1;
        }
        return result;
    }


    public static int[] generateRandomMatrixII(int rowSize) {
        if (rowSize < 0) {
            return null;
        }
        int[] result = new int[rowSize];
        for (int i = 0; i != result.length; i++) {
            if (i % 2 == 0) {
                result[i] = (int) (Math.random() * 100);
            } else {
                result[i] = ~(int) (Math.random() * 100);
            }

        }
        return result;
    }

    public static int[] copyArr(int[] arr) {
        int[] list = new int[arr.length];
        for (int i = 0; i < list.length; i++) {
            list[i] = arr[i];
        }
        return list;
    }


    public static void swap(int[] arr, int j, int i) {
        int swap = arr[j];
        arr[j] = arr[i];
        arr[i] = swap;
    }

    // for test
    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i != matrix.length; i++) {
            System.out.print("{");
            for (int j = 0; j != matrix[0].length; j++) {
                String temp = j == matrix[0].length - 1 ? "" : ",";
                System.out.print(matrix[i][j] + temp);
            }
            System.out.println("},");
        }
    }

    public static void printMatrix(int[] matrix) {
        System.out.print("{");

        for (int i = 0; i != matrix.length; i++) {
            System.out.print(matrix[i] + ",");
        }
        System.out.println("}");

    }


}
